package yxy.game.pm2.command.godwar;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.GodWarMessage;
import yxy.game.pm2.module.arena.RankArena;
import yxy.game.pm2.module.godwar.GodWarAreaModule;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.Map;

/** 副本战神殿 战神殿 */
@MessageType(proto = Command.GodWar, classtype = GodWarMessage.GodWarRequest.class)
public class CMD_GodWar extends Command<GodWarMessage.GodWarRequest> {

	@Override
	public void execute(MessageSource messageSource, GodWarMessage.GodWarRequest request) {
		String playerCode = messageSource.getTag();

		GodWarMessage.GodWarResponse.Builder response = GodWarMessage.GodWarResponse.newBuilder();

		Map<Integer, GodWarAreaModule> modules = GodWarAreaModule.get(playerCode);

		for (GodWarAreaModule module : modules.values()) {
			GodWarMessage.GodWar.Builder godwar = GodWarMessage.GodWar.newBuilder();
			godwar.setId(module.getDataId());
			godwar.setEvolve(module.getEvolve());
			// 封装占有者信息
			if (module.getOccupantCode() != null) {
				godwar.setOccupant(new PlayerHandle().playerSimpleInfo(module.getOccupantCode()));
			}
			response.addInfo(godwar);
		}
		response.setRank(RankArena.getRank(playerCode).rank(playerCode));

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, GodWarMessage.GodWarResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}